package sms_interface;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.*;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class GetSMS {

	/**
	 * @param args
	 */
	@SuppressWarnings("deprecation")
	static DataOutputStream smsout;
	public static void main(String[] args){

		
		while(true)
		{
			try
			{
				smsout = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("smsout.txt",true)));
			}
			catch(Exception exp)
			{}

			try{
			Runtime r = Runtime.getRuntime();
			Process p  = r.exec("wget --no-check-certificate https://admin:99admin11@192.168.1.175/admin/forms/sms-read.html");
			p.waitFor();
			File F = new File("sms-read.html");
			DataInputStream din = new DataInputStream(new FileInputStream(F));
			String data = "";
			String arr[] = null;
			
			try
			{
				while(true)
				{
					data = din.readLine();
					Date d ;
					if(data.contains("<script>linie=new Array("))
					{
						arr = data.split(",");
						String dateinstr = arr[0].substring(25, 42);
						d = strtodate(dateinstr);
						String truedate = d +" " +dateinstr.substring(9);
						String messageno[] = arr[3].split("/");
						String count = messageno[3].substring(0,7);
						String number = arr[1].substring(3,arr[1].length()-1);
						String message = arr[2].substring(0, arr[2].length()-21);
						if(count.charAt(0) == '-')
						{
							count = messageno[3].substring(0,8);
						}
						System.out.println("count = " + count);
						System.out.println("message = " + arr[2].substring(0, arr[2].length()-21));
						System.out.println("number = " + arr[1].substring(3,arr[1].length()-2));
						p = r.exec("wget --no-check-certificate https://admin:99admin11@192.168.1.175/admin/forms/sms-read.html?id=/out/sms/"+count+"&send=del");
						p.waitFor();
						File D = new File("sms-read.html?id=%2Fout%2Fsms%2F"+count+"&send=del");
						D.delete();
						smsout.writeBytes(URLDecoder.decode(message) + "---sent_by---" + number+ "---at---" + truedate+"\n");
						smsout.writeBytes("*** New SMS ***\n");
						
						/*try
						{
							while(true)
							{
								smsout.writeline(din.readline());
							}
						}
						catch(Exception exp)
						{
							break;
						}*/
					}
				}
			}
			catch(Exception e)
			{
				din.close();
			}
			F.delete();
			smsout.close();
			}
			catch(Exception exp)
			{
				exp.printStackTrace();
			}
			try{
			Thread.sleep(30000);
			}
			catch(Exception ex){}
		}
	}

public static java.sql.Date strtodate(String val)
	{
		java.util.Date date = new java.util.Date();
		try{
			DateFormat formatter ;
	        formatter = new SimpleDateFormat("M.dd.yy H:m:s");
	        date = (Date)formatter.parse(val);
	    }
		
		catch (ParseException e)
	    {
	    	System.out.println("Exception :"+e);    
	    }    
		java.sql.Date d = new java.sql.Date(date.getTime());
		return d;
	}

}



